import { message } from 'antd';
import { formatMessage } from 'umi/locale';
import {
  queryProduct,
  queryDeviceCount,
  queryDevice,
  del,
  add,
  batch,
  queryGroups,
  queryDetail,
  edit,
  queryTopics,
  delTopic,
  addTopic,
  addChildDevice,
  queryDeviceList,
  delChildDev,
  queryDeviceExtend,
  saveOrEditDeviceExtend,
  queryDeviceProps,
  queryDeviceServices,
  serviceExecute,
  qryProtocolInfo,
  editProtocol,
  queryDecoder,
  queryEncoder,
  queryModbusProtocol,
  queryModbusDataType,
  queryRegisterType,
  addModbusProtocol,
  delModbusProtocol,
  editModbusProtocol,
  queryModbusDetail,
} from '@/services/devices';

export default {
  namespace: 'devices',

  state: {
    products: [],
    list: [],
    pagination: { pageSize: 10 },
    current: {},
    deviceCount: 0,
    activateCount: 0,
    groupList: [],
    detail: {},
    topics: [],
    deviceList: [],
    deviceExtend: {},
    props: [],
    services: [],
    protocolInfo: {},
    decoder: [],
    encoder: [],
    modbusProtocolList: [],
    registerTypes: [],
    dataTypes: [],
    modbusDetail: {},
  },

  effects: {
    *init({ payload }, { call, put }) {
      const products = yield call(queryProduct);
      if (products.httpCode === 200) {
        yield put({
          type: 'saveProducts',
          payload: products,
        });
      } else {
        message.error({ content: products.msg, key: 1 });
      }
      const data = yield call(queryDeviceCount, payload);
      if (data.httpCode === 200) {
        yield put({
          type: 'saveDeviceCount',
          payload: data,
        });
      } else {
        message.error({ content: data.msg, key: 1 });
      }
      const devices = yield call(queryDevice, payload);
      if (devices.httpCode === 200) {
        yield put({
          type: 'save',
          payload: devices,
        });
      } else {
        message.error({ content: devices.msg, key: 1 });
      }
    },
    *fetch({ payload }, { call, put }) {
      const devices = yield call(queryDevice, payload);
      if (devices.httpCode === 200) {
        yield put({
          type: 'save',
          payload: devices,
        });
      } else {
        message.error({ content: devices.msg, key: 1 });
      }
    },
    *fetchProduct({ payload }, { call, put }) {
      const response = yield call(queryProduct, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveProducts',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchAndCount({ payload }, { call, put }) {
      const devices = yield call(queryDevice, payload);
      if (devices.httpCode === 200) {
        yield put({
          type: 'save',
          payload: devices,
        });
      } else {
        message.error({ content: devices.msg, key: 1 });
      }
      const data = yield call(queryDeviceCount, payload);
      if (data.httpCode === 200) {
        yield put({
          type: 'saveDeviceCount',
          payload: data,
        });
      } else {
        message.error({ content: data.msg, key: 1 });
      }
    },
    *refresh({ payload }, { call, put }) {
      const devices = yield call(queryDevice, payload);
      const data = yield call(queryDeviceCount, payload);
      if (devices.httpCode === 200) {
        yield put({
          type: 'save',
          payload: devices,
        });
      } else {
        message.error({ content: devices.msg, key: 1 });
      }
      if (data.httpCode === 200) {
        yield put({
          type: 'saveDeviceCount',
          payload: data,
        });
      } else {
        message.error({ content: data.msg, key: 1 });
      }
      if (devices.httpCode === 200 && data.httpCode === 200) {
        message.success(formatMessage({ id: 'app.dm.devices.refresh.success' }));
      }
    },
    *del({ payload }, { call, put }) {
      const response = yield call(del, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({ type: 'init' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *add({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(add, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({ type: 'init' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *batch({ payload }, { call, put }) {
      const response = yield call(batch, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({ type: 'init' });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchGroups({ payload }, { call, put }) {
      const response = yield call(queryGroups, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveGroups',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(queryDetail, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *edit({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(edit, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchDetail',
          payload: payload.id,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addTopic({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(addTopic, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'topics',
          payload: payload.deviceId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *topics({ payload }, { call, put }) {
      const response = yield call(queryTopics, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveTopics',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delTopic({ payload }, { call, put }) {
      const response = yield call(delTopic, payload.id);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'topics',
          payload: payload.deviceId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDevices({ payload }, { call, put }) {
      const response = yield call(queryDeviceList, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDevices',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *addChildDevice({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(addChildDevice, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetch',
          payload: { pid: payload.pid },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *delChildDev({ payload }, { call, put }) {
      const response = yield call(delChildDev, payload.childDevId);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetch',
          payload: { pid: payload.pid },
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDeviceExtend({ payload }, { call, put }) {
      const response = yield call(queryDeviceExtend, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDeviceExtend',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *saveOrEditDeviceExtend({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(saveOrEditDeviceExtend, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchDeviceExtend',
          payload: payload.deviceId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDeviceProps({ payload }, { call, put }) {
      const response = yield call(queryDeviceProps, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDeviceProps',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDeviceServices({ payload, changeServices }, { call }) {
      const response = yield call(queryDeviceServices, payload);
      if (response.httpCode === 200) {
        // yield put({
        //   type: 'saveDeviceServices',
        //   payload: response,
        // });
        changeServices(response.data);
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *serviceExecute({ payload }, { call }) {
      const response = yield call(serviceExecute, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchProtocol({ payload }, { call, put }) {
      const response = yield call(qryProtocolInfo, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveProtocolInfo',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *editProtocol({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(editProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchProtocol',
          payload: payload.deviceId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDecoder({ payload }, { call, put }) {
      const response = yield call(queryDecoder, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveDecoder',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchEncoder({ payload }, { call, put }) {
      const response = yield call(queryEncoder, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveEncoder',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },

    *fetchModbusProtocol({ payload }, { call, put }) {
      const response = yield call(queryModbusProtocol, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveModbusProtocol',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchDataType({ payload }, { call, put }) {
      const response = yield call(queryModbusDataType, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveModbusDataType',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchRegisterType({ payload }, { call, put }) {
      const response = yield call(queryRegisterType, payload);
      if (response.httpCode === 200) {
        yield put({
          type: 'saveRegisterType',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *addModbusProtocol({ payload, form, handleModalVisible }, { call, put }) {
      const response = yield call(addModbusProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleModalVisible();
        yield put({
          type: 'fetchModbusProtocol',
          payload: payload.protocolId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *delModbusProtocol({ payload, modbusProtocolId }, { call, put }) {
      const response = yield call(delModbusProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        yield put({
          type: 'fetchModbusProtocol',
          payload: modbusProtocolId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *editModbusProtocol({ payload, form, handleEditModalVisible }, { call, put }) {
      const response = yield call(editModbusProtocol, payload);
      if (response.httpCode === 200) {
        message.success(response.msg);
        form.resetFields();
        handleEditModalVisible();
        yield put({
          type: 'fetchModbusProtocol',
          payload: payload.protocolId,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
    *fetchModbusDetail({ payload, handleEditModalVisible }, { call, put }) {
      const response = yield call(queryModbusDetail, payload);
      if (response.httpCode === 200) {
        handleEditModalVisible(true);
        yield put({
          type: 'saveModbusDetail',
          payload: response,
        });
      } else {
        message.error({ content: response.msg, key: 1 });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        list: action.payload.data,
        pagination: {
          current: action.payload.current,
          pageSize: action.payload.size,
          total: action.payload.iTotalRecords,
        },
      };
    },
    saveProducts(state, action) {
      return {
        ...state,
        products: action.payload.data,
      };
    },
    saveDeviceCount(state, action) {
      return {
        ...state,
        deviceCount: action.payload.data.deviceCount,
        activateCount: action.payload.data.activateCount,
      };
    },
    saveGroups(state, action) {
      return {
        ...state,
        groupList: action.payload.data,
      };
    },
    saveDetail(state, action) {
      return {
        ...state,
        detail: action.payload.data,
      };
    },
    saveTopics(state, action) {
      return {
        ...state,
        topics: action.payload.data,
      };
    },
    saveDevices(state, action) {
      return {
        ...state,
        deviceList: action.payload.data,
      };
    },
    saveDeviceExtend(state, action) {
      return {
        ...state,
        deviceExtend: action.payload.data,
      };
    },
    saveDeviceProps(state, action) {
      return {
        ...state,
        props: action.payload.data,
      };
    },
    saveDeviceServices(state, action) {
      return {
        ...state,
        services: action.payload.data,
      };
    },
    saveProtocolInfo(state, action) {
      return {
        ...state,
        protocolInfo: action.payload.data,
      };
    },
    saveDecoder(state, action) {
      return {
        ...state,
        decoder: action.payload.data,
      };
    },
    saveEncoder(state, action) {
      return {
        ...state,
        encoder: action.payload.data,
      };
    },
    saveModbusProtocol(state, action) {
      return {
        ...state,
        modbusProtocolList: action.payload.data,
      };
    },
    saveModbusDataType(state, action) {
      return {
        ...state,
        dataTypes: action.payload.data,
      };
    },
    saveRegisterType(state, action) {
      return {
        ...state,
        registerTypes: action.payload.data,
      };
    },
    saveModbusDetail(state, action) {
      return {
        ...state,
        modbusDetail: action.payload.data,
      };
    },
  },
};
